<?php
/**
 * Created by PhpStorm.
 * User: zeopean
 * Date: 2015/8/20
 * Time: 11:03
 */

namespace App\Http\Controllers\Api;

use App\Exceptions\FocusException;
use App\Exceptions\InvestorException;
use App\FocusInvestor;
use App\FocusInvestOrg;
use App\FocusProject;
use App\Investor;
use App\Project;
use App\User;
use Illuminate\Auth\Guard;
use App\Exceptions\SecurityException;

class FocusController extends ApiController{
    /**
     * 关注项目
     *
     * @param Guard $guard
     * @param int $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws FocusException
     * @throws SecurityException
     */
    public function IsFocusProject(Guard $guard , $id=0 )
    {
        /** 判断用户是否登录*/
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if($id==0 || is_null($id))
            throw new FocusException(FocusException::NoProjectId);

        /** 获取用户标识 */
        $user_id = $guard->user()->id;
        if(is_null($user_id))
            throw new FocusException(FocusException::NoAuthFocusProject);

        $flag = User::find($user_id)->userFocusProject()->wherePivot('project_id','=',$id)->first();
        if($flag){
            User::find($user_id)->userFocusProject()->detach($id);
            return $this->buildResponse(trans('focus.cancel-focus-project.success'));
        }else{
            User::find($user_id)->userFocusProject()->attach($id);
            return $this->buildResponse(trans('focus.focus-project.success'), User::find($user_id)->userFocusProject());
        }
    }

    /**
     * 关注投资人
     *
     * @param Guard $guard
     * @param int $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws FocusException
     * @throws SecurityException
     */
    public function IsFocusInvestor(Guard $guard , $id=0 )
    {
        /** 判断用户是否登录*/
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if($id==0 || is_null($id))
            throw new FocusException(FocusException::NoInvestorId);

        /** 获取用户标识 */
        $user_id = $guard->user()->id;
        if(is_null($user_id))
            throw new FocusException(FocusException::NoAuthFocusInvestor);

        /** @var 判断 关注user_id 和 investor_id 是否返回条件 $flag */
        $flag = User::find($user_id)->userFocusInvestor()->wherePivot('investor_id','=',$id)->first();
        if($flag){
            User::find($user_id)->userFocusInvestor()->detach($id);
            return $this->buildResponse(trans('focus.cancel-focus-investor.success'));
        }else{

            User::find($user_id)->userFocusInvestor()->attach($id);
            return $this->buildResponse(trans('focus.focus-investor.success') , User::find($user_id)->userFocusInvestor);
        }

    }

    /**
     * 是否关注投资机构
     *
     * @param Guard $guard
     * @param int $id
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     * @throws FocusException
     * @throws SecurityException
     */
    public function IsFocusInvestOrg(Guard $guard , $id=0 )
    {
        /** 判断用户是否登录*/
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User  $currentUser */
        $currentUser = $guard->user();
        if(!$currentUser)
            throw new SecurityException(SecurityException::NoPermission);

        if($id==0 || is_null($id))
            throw new FocusException(FocusException::NoInvestOrgId);

        /** 获取用户标识 */
        $user_id = $guard->user()->id;
        if(is_null($user_id))
            throw new FocusException(FocusException::NoAuthFocusInvestOrg);

        $user = User::find($user_id);
        if($user->IsFocusInvestOrg($id)){
            User::find($user_id)->userFocusInvestOrg()->detach($id);
            return $this->buildResponse(trans('focus.cancel-focus-invest-org.success'));
        }else{
            User::find($user_id)->userFocusInvestOrg()->attach($id);
            return $this->buildResponse(trans('focus.focus-invest-org.success') , User::find($user_id) ->userFocusInvestOrg);
        }
    }
}